package com.ustadzmenjawab.client;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.google.gwt.view.client.ListDataProvider;
import com.ustadzmenjawab.client.images.WebImages;
import com.ustadzmenjawab.shared.SearchResult;
import com.ustadzmenjawab.shared.WebsiteSource;


public class AboutUsPage extends Composite {
	private DockPanel basePanel;
	private DockPanel northPanel;
	private DockPanel leftPanel;
	//private HTML centralHtml ;
	private DockPanel centralPanel;
	private ScrollPanel centrallScroll;
	private  AsyncCallback aboutUsCallback = new AsyncCallback(){
		@Override
		public void onFailure(Throwable caught) {
			showErrorDialog("Mohon maaf, terdapat masalah teknis dalam pencarian.");
		}

		@Override
		public void onSuccess(Object result) {
			String opening = "<h2>Tentang Kami</h2>";
			String content  = (String)result;
			if(centralPanel != null){
				centralPanel.clear();
				
			}
			HTML centralHtml = new HTML();
			centralHtml.setHTML(opening+content);
			centralPanel.add(centralHtml, DockPanel.CENTER);
			//basePanel.add(centralPanel, DockPanel.CENTER);
			
		}
	};

	private  AsyncCallback showSourceCallback = new AsyncCallback(){
		@Override
		public void onFailure(Throwable caught) {
			showErrorDialog("Mohon maaf, terdapat masalah teknis dalam pencarian.");
		}

		@Override
		public void onSuccess(Object result) {
			List<WebsiteSource> content  = (List<WebsiteSource>)result;
			String htmlOpening = "<h2>Daftar Sumber</h2>" +
			"<p align=\"centre\">Berikut ini daftar "+content.size()+" website untuk sumber search-engine kami.</p>";
			if(centralPanel != null)
				basePanel.remove(centralPanel);
		    centralPanel = new DockPanel();
		    HTML centralHtml = new HTML(htmlOpening);
			centralPanel.add(centralHtml, DockPanel.NORTH);
			CellTable<WebsiteSource> table = new CellTable<WebsiteSource>();
			TextColumn<WebsiteSource> urlColumn = new TextColumn<WebsiteSource>() {
				@Override
				public String getValue(WebsiteSource source) {
					return source.getWebsiteUrl();
				}
			};
			urlColumn.setSortable(true);

			TextColumn<WebsiteSource> ustadzColumn = new TextColumn<WebsiteSource>() {
				@Override
				public String getValue(WebsiteSource source) {
					return source.getWebsiteName();
				}
			};
			
			// Add the columns.
		    table.addColumn(urlColumn, "Website");
		    table.addColumn(ustadzColumn, "Ustadz");
		    table.setVisibleRange(0, content.size());
		    // Create a data provider.
		    ListDataProvider<WebsiteSource> dataProvider = new ListDataProvider<WebsiteSource>();
		    dataProvider.addDataDisplay(table);
		    
		    List<WebsiteSource> list = dataProvider.getList();
		    for (WebsiteSource contact : content) {
		      list.add(contact);
		    }
		    
		    ListHandler<WebsiteSource> columnSortHandler = new ListHandler<WebsiteSource>(
		            list);
		        columnSortHandler.setComparator(urlColumn,
		            new Comparator<WebsiteSource>() {
		              public int compare(WebsiteSource o1, WebsiteSource o2) {
		                if (o1 == o2) {
		                  return 0;
		                }

		                // Compare the name columns.
		                if (o1 != null) {
		                  return (o2 != null) ? o1.getWebsiteUrl().compareTo(o2.getWebsiteUrl()) : 1;
		                }
		                return -1;
		              }
		            });
		        table.addColumnSortHandler(columnSortHandler);

		        // We know that the data is sorted alphabetically by default.
		        table.getColumnSortList().push(urlColumn);
		        
		        centralPanel.add(table,DockPanel.CENTER);
		        centralPanel.setCellHorizontalAlignment(table, HasAlignment.ALIGN_CENTER);
		        basePanel.add(centralPanel, DockPanel.CENTER);
		}
	};	
	public AboutUsPage() {

		basePanel = new DockPanel();
		northPanel = new DockPanel();
		leftPanel = new DockPanel();
		centrallScroll = new ScrollPanel();
		int scrWidthResolution = SearchResultPage.getScreenWidth();
		int scrHeightResolution = SearchResultPage.getScreenHeight();
		WebImages webImage = (WebImages) GWT.create(WebImages.class);
		Image logo = null;
		if(scrWidthResolution >= 1200){
			logo = webImage.ustadzmenjawab_1280().createImage();
		}else if(scrWidthResolution >= 1000){
			logo = webImage.ustadzmenjawab_1024().createImage();
		}else if(scrWidthResolution >= 768){
			logo = webImage.ustadzmenjawab_800().createImage();
		}
		logo.addClickHandler(new LogoClickHandler());
		northPanel.add(logo,DockPanel.WEST);
		basePanel.add(northPanel, DockPanel.NORTH);

		HTML centralHtml = new HTML("", true);
	    centralPanel = new DockPanel();
	    centralPanel.add(centralHtml,DockPanel.CENTER);
	    
	    centrallScroll.add(centralPanel);
		basePanel.add(centralPanel, DockPanel.CENTER);

		VerticalPanel pageListPanel = new VerticalPanel();
		HTML placeHolderHtml = new HTML("&nbsp;", true);

		Anchor hprlnkHalamanUtama = new Anchor("Halaman Utama", true, "http://www.ustadzmenjawab.com");
		pageListPanel.add(hprlnkHalamanUtama);

		Anchor hprlnkTentangKami = new Anchor("Tentang Kami");
		pageListPanel.add(hprlnkTentangKami);
		hprlnkTentangKami.addClickHandler(new AboutUSClickHandler());

		Anchor hprlnkDaftarSumber = new Anchor("Daftar Sumber");
		pageListPanel.add(hprlnkDaftarSumber);
		hprlnkDaftarSumber.addClickHandler(new ShowSourceClickHandler());
		
		leftPanel.add(pageListPanel, DockPanel.NORTH);
		leftPanel.add(placeHolderHtml,DockPanel.CENTER);
		basePanel.add(leftPanel, DockPanel.WEST);
		basePanel.setCellWidth(leftPanel, SearchResultPage.LEFT_PANEL_WIDTH);
		initWidget(basePanel);
		showAboutUs();
	}

	public void showAboutUs(){
		AppControlServiceAsync appService = ServerConnect.getAppControlService();
		appService.getPage("about_us",aboutUsCallback);
	}

	public void showSourceList(){
		AppControlServiceAsync appService = ServerConnect.getAppControlService();
		appService.getSourceList(showSourceCallback);
	}


	class AboutUSClickHandler implements ClickHandler{

		@Override
		public void onClick(ClickEvent event) {
			showAboutUs();

		}

	}
	
	class ShowSourceClickHandler implements ClickHandler{

		@Override
		public void onClick(ClickEvent event) {
			showSourceList();
		}
		
	}
	public void showErrorDialog(String errMsg){

		Window.alert(errMsg);
	}
}
